import numpy as np
import numpy.linalg as nm 
from math import sqrt
import partie1 as p1

def bidiagonal_transformation(M):
    """Function that take in parameter a matrix and transforms it to a bidiagonal one"""
    n=M.shape[0]                             #store the shape of the matrix     
    Ql=np.eye(n,n)                           #initialize the Qleft and Qright factors at identity matrix 
    Qr=np.eye(n,n)
    for i in range(n-1):                     
        Y=np.asarray(M[i:n,i])
        X=np.asarray(np.zeros([Y.size,1]))
        X[0]=nm.norm(Y)
        L=p1.householder_matrix(Y,X)[1]
        N=np.eye(n,n)
        N[i:n,i:n]=L
        M=np.dot(N,M)
        Ql=np.dot(Ql,N)
        if( i!=n-2):
            Y=np.asarray(M[i,i+1:n])
            X=np.asarray(np.zeros([Y.size,1]))
            X[0]=nm.norm(Y)
            L=p1.householder_matrix(Y,X)[1]
            N=np.eye(n,n)
            N[i+1:n,i+1:n]=L
            M=np.dot(M,N)
            Qr=np.dot(N,Qr)
        
#        print np.dot(Ql,np.dot(M,Qr))
    
    return (Ql,M,Qr)

#M=np.matrix([[5,6,3,2],[4,5,2,3],[6,2,3,6],[3,0,1,1]])
#print "                 Test sur la partie 2 : \n"
#print "Matrice de test : "
#print M

#print "M sous forme bidiagonale : " 
#print bidiagonal_transformation(M)[1]
#print "Changement de base a gauche : "
#print bidiagonal_transformation(M)[0]
#print "changement de base a droite : "
#print bidiagonal_transformation(M)[2]
print "\n"


    
    
